<?php

	class MySqlDb
	{
		//Atributos
		private $db_connection;
		private $db_server;
		private $db_user;
		private $db_password;
		private $db_base;
				
		//Construtor
		public function __construct($db_server, $db_user, $db_password, $db_base)
		{
			$this->db_server = $db_server;
			$this->db_user = $db_user;
			$this->db_password = $db_password;
			$this->db_base = $db_base;
		}
		
		//Funcoes//////
		//Abrir conexao com DB
		public function connect()
		{
			$this->db_connection = $db_connection = mysql_connect($this->db_server, $this->db_user, $this->db_password);
			
			//Caso haja problema	
			if(! $this->getDbConnection())
				die("Erro na conexao com MySql" . mysql_error());
				
			$this->selectDatabase($this->db_base);
		}	
		//Fechar conexao DB
		public function close()
		{
			if( $this->getDbConnection())
				mysql_close($this->getDbConnection());
		}
		
		
		//Executar instrucao SQL
		public function executeQuery($db_query)
		{
			$db_result = mysql_query($db_query, $this->getDbConnection());
			
			if(! $db_result )
				die("Erro na execucao da instrucao" . mysql_error());
				
			return $db_result;
		}
			
		//Retornar o proximo registro a partir do resultado informado
		public function next($db_result)
		{
			return mysql_fetch_array($db_result);
		
		}
		
		
		//Retornar a quantidade de linhas
		public function countRows($db_result)
		{
			return mysql_num_rows($db_result);
		}
		
		//Getters e Setters
		public function getDbConnection()
		{
			return $this->db_connection;
		}
		
		public function selectDataBase($db_base)
		{
			mysql_select_db($db_base) or
				die ("Banco de Dados $db_database nao pode ser acessado".mysql_error());
		}	
	}




?>
